package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmeaService;
import com.itheima.reggie.service.SetmealDishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

@Slf4j
@Service
public class SetmeaServiceimpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmeaService {

    @Autowired
    private SetmealDishService setmeaService;

    @Override
    public IPage<Setmeal> selectPage(Integer page, Integer pageSize, String name) {
        IPage<Setmeal> page1 = new Page<>(page,pageSize);

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(null!=name,Setmeal::getName,name).orderByDesc(Setmeal::getUpdateTime);
        IPage<Setmeal> page2 = this.page(page1, queryWrapper);
        return page2;
    }

    /**
     * 保存套餐
     */
    @Override
    public boolean saveSetmealDto(SetmealDto setmealDto) {
        //存入基本值
        this.save(setmealDto);
        //获取套餐和菜品的关系对象
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        Iterator<SetmealDish> iterator = setmealDishes.iterator();
        while (iterator.hasNext()==true) {
            iterator.next().setSetmealId(setmealDto.getId());
        }

        boolean b = setmeaService.saveBatch(setmealDishes);
        return b;
    }

    /**
     * 批量停售起售
     */
    @Override
    public boolean updateStatus(Integer status, List<Long> ids) {

        UpdateWrapper<Setmeal> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .in(Setmeal::getId, ids)
                .set(Setmeal::getStatus,status);

        boolean res= this.update(updateWrapper);


        return res;
    }

    /**
     * 批量删除
     */
    @Override
    public boolean deletests(List<Long> ids) {

        boolean b = this.removeByIds(ids);
        return b;
    }
}
